home *** CD-ROM | disk | FTP | other *** search
/ PC Format (PL) 2008 March / PCFormat 3_2008.iso / Multimedia / Miro 1.0 / Miro_Installer.exe / moviedata_util.py < prev    next >
Text File  |  2007-11-12  |  5KB  |  160 lines

  1. # Miro - an RSS based video player application
  2. # Copyright (C) 2005-2007 Participatory Culture Foundation
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 2 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
  17.  
  18. import ctypes
  19. from ctypes import byref
  20. import glob
  21. import os
  22. import sys
  23. from time import sleep, time
  24.  
  25. path_list = os.environ.get('PATH', '').split(';')
  26. path_list.insert(0, 'xulrunner\\plugins\\')
  27. os.environ['PATH'] = ';'.join(path_list)
  28.  
  29. # load the DLL
  30. libvlc = ctypes.cdll.libvlc
  31. # set up the function signatures
  32. libvlc.libvlc_new.restype = ctypes.c_void_p
  33. libvlc.libvlc_playlist_get_input.restype = ctypes.c_void_p
  34. libvlc.libvlc_input_get_position.restype = ctypes.c_float
  35. libvlc.libvlc_input_get_length.restype = ctypes.c_longlong
  36.  
  37. class VLCException(ctypes.Structure):
  38.     _fields_ = [
  39.             ('b_raised', ctypes.c_int),
  40.             ('psz_message', ctypes.c_char_p)
  41.     ]
  42.  
  43. exception = VLCException() 
  44. # global exception object
  45.  
  46. libvlc.libvlc_exception_init(byref(exception))
  47.  
  48. class VLCError(Exception):
  49.     pass
  50.  
  51. def check_exception():
  52.     if exception.b_raised:
  53.         msg = exception.psz_message
  54.         libvlc.libvlc_exception_clear(byref(exception))
  55.         raise VLCError(msg)
  56.  
  57. def make_string_list(*args):
  58.     ArgsArray = ctypes.c_char_p * len(args)
  59.     return ArgsArray(*args)
  60.  
  61. def init_vlc(*args):
  62.     arg_pointers = make_string_list(*args)
  63.     vlc = libvlc.libvlc_new(len(arg_pointers), arg_pointers, byref(exception))
  64.     check_exception()
  65.     return vlc
  66.  
  67. vlc = init_vlc( "vlc", "--noaudio", 
  68.     '--vout', 'image', 
  69.     '--quiet', '--nostats', '--intf', 'dummy', '--plugin-path', 'vlc-plugins')
  70.  
  71. def setup_playlist(video_path, thumbnail_path):
  72.     thumb_split = os.path.splitext(thumbnail_path)
  73.     options = make_string_list('image-out-prefix=%s-temp' % thumb_split[0],
  74.             'image-out-format=%s' % thumb_split[1][1:])
  75.     libvlc.libvlc_playlist_add_extended(vlc, video_path, None, len(options),
  76.             options, byref(exception))
  77.     check_exception()
  78.     libvlc.libvlc_playlist_play(vlc, -1, 0, None, byref(exception))
  79.     check_exception()
  80.  
  81. def wait_for_input():
  82.     while True:
  83.         input = libvlc.libvlc_playlist_get_input(vlc, None)
  84.         if input != None:
  85.             return input
  86.  
  87. def wait_for_vout(input):
  88.     starttime = time()
  89.     while True:
  90.         if time() - starttime > 4.0:
  91.             return False
  92.         vout_exists = libvlc.libvlc_input_has_vout(input, byref(exception))
  93.         check_exception()
  94.         if vout_exists:
  95.             return True
  96.         sleep(0.1)
  97.  
  98. def temp_snapshot_path(thumbnail_path, index):
  99.     start, ext = os.path.splitext(thumbnail_path)
  100.     return '%s-temp%.6i%s' % (start, index, ext)
  101.  
  102. def delete_temp_snapshots(path):
  103.     start, ext = os.path.splitext(path)
  104.     temp_snapshots = glob.glob('%s-temp*%s' % (start, ext))
  105.     for path in temp_snapshots:
  106.         try:
  107.             os.remove(path)
  108.         except:
  109.             pass
  110.  
  111. def wait_for_snapshot(thumbnail_path):
  112.     while True:
  113.         if os.path.exists(temp_snapshot_path(thumbnail_path, 1)):
  114.             # check for the second snapshot because that means the 1st
  115.             # snapshot is definitely done writing
  116.             os.rename(temp_snapshot_path(thumbnail_path, 0), thumbnail_path)
  117.             break
  118.         input = libvlc.libvlc_playlist_get_input(vlc, None)
  119.         if input is None:
  120.             break
  121.         sleep(0.1)
  122.  
  123. def stop_input():
  124.     libvlc.libvlc_playlist_clear(vlc, byref(exception))
  125.     check_exception()
  126.     #libvlc.libvlc_playlist_stop(vlc, byref(exception))
  127.     #check_exception()
  128.     while True:
  129.         input = libvlc.libvlc_playlist_get_input(vlc, None)
  130.         if input is None:
  131.             break
  132.         sleep(0.1)
  133.  
  134. def make_snapshot(video_path, thumbnail_path):
  135.     if os.path.exists(thumbnail_path):
  136.         os.remove(thumbnail_path)
  137.     setup_playlist(video_path, thumbnail_path)
  138.     input = wait_for_input()
  139.     libvlc.libvlc_input_set_position(input, ctypes.c_float(0.5), byref(exception))
  140.     check_exception()
  141.     if wait_for_vout(input):
  142.         wait_for_snapshot(thumbnail_path)
  143.     time = libvlc.libvlc_input_get_length(input, byref(exception))
  144.     check_exception()
  145.     stop_input()
  146.     delete_temp_snapshots(thumbnail_path)
  147.     print "Miro-Movie-Data-Length: %d" % (time)
  148.     if os.path.exists(thumbnail_path):
  149.         print "Miro-Movie-Data-Thumbnail: Success"
  150.     else:
  151.         print "Miro-Movie-Data-Thumbnail: Failure"
  152.  
  153. if __name__ == '__main__':
  154.     try:
  155.         make_snapshot(sys.argv[1], sys.argv[2])
  156.     except Exception, e:
  157.         print e
  158.         print "Miro-Movie-Data-Length: -1"
  159.         print "Miro-Movie-Data-Thumbnail: Failure"
  160.